<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Wall
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: AdminSettingController.php 2011-04-26 11:18:13 michael $
 * @author     Michael
 */

/**
 * @category   Application_Extensions
 * @package    Wall
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Wall_AdminSettingController extends Core_Controller_Action_Admin {
	public function init() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'wall_admin_main', array (), 'wall_admin_main_setting' );
	}
	
	public function indexAction() {
		$setting = Engine_Api::_ ()->getDbTable ( 'settings', 'core' );
		
		$this->view->form = $form = new Engine_Form ();
		
		$facebook = new Wall_Form_Service_Facebook ();
		$form->addSubForm ( $facebook, 'facebook' );
		
		$twitter = new Wall_Form_Service_Twitter ();
		$form->addSubForm ( $twitter, 'twitter' );
		
		foreach ( $form->getSubForms () as $subform ) {
			$subform->getDecorator ( 'Description' )->setOption ( 'escape', false );
		}
		
		$form->addElement ( 'Button', 'submit', array ('label' => 'Save Changes', 'ignore' => true, 'type' => 'submit' ) );
		
		$setting_values = $setting->getSetting ( 'wall.service' );
		if (! $setting_values) {
			$setting_values = array ();
		}
		$form->populate ( $setting_values );
		
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		$values = $form->getValues ();
		if (! $values) {
			$values = array ();
		}
		foreach ( $values as $key => $value ) {
			if (! $value || ! is_array ( $value )) {
				continue;
			}
			foreach ( $value as $sub_key => $sub_value ) {
				if ($sub_value === null) {
					$values [$key] [$sub_key] = $setting->getSetting ( 'wall.service.' . $key . '.' . $sub_key );
				} else {
					$setting->setSetting ( 'wall.service.' . $key . '.' . $sub_key, $sub_value );
					$values [$key] [$sub_key] = $setting->getSetting ( 'wall.service.' . $key . '.' . $sub_key );
				}
			}
		}
		
		$form->populate ( $values );
		$form->addNotice ( 'Your changes have been saved.' );
	
	}

}